package com.ebookstore.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.ebookstore.dto.UserInfo;
import com.ebookstore.struts.form.UserForm;

public class UserAction extends BaseAction {
	/**
	 * 转向用户管理页面
	 */
	public ActionForward selfService(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		return mapping.findForward("self");
	}
	/**
	 * 用户注册
	 */
	public ActionForward register(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		return mapping.findForward("form");
	}
	/**
	 * 保存用户
	 */
	public ActionForward saveUser(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		System.out.println("useraction.saveuser()");
		UserForm userForm = (UserForm) form;
		UserInfo user = null;
		//如果id=1表示为新建否则为更新
		if (userForm.getId() == -1) {
			System.out.println("userForm.getId() == -1");
			user = new UserInfo();
		} else {
			user = this.getEBookStore().getUserService().findUserById(
					userForm.getId());
		}
		//如果用户不存在，则提示错误信息
		if (user == null) {
			request.setAttribute("message", "无法保存用户");
			return mapping.findForward("failure");
		}
		
		//设置用户信息
		user.setAddress(userForm.getAddress());
		user.setEmail(userForm.getEmail());
		user.setUserName(userForm.getUserName());
		user.setName(userForm.getName());
		user.setPassword(userForm.getPassword());
		user.setPhone(userForm.getPassword());
		user.setSex(userForm.getSex());
		
		//保存用户
		if (userForm.getId() == -1) {
			System.out.println("useraction.getEBookStore().getUserService().addUser(user)");
			getEBookStore().getUserService().addUser(user);
			
		} else {
			getEBookStore().getUserService().updateUser(user);
		}
		
		return mapping.findForward("self");
	}
	/**
	 * 编辑用户信息
	 */
	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		UserForm userForm = (UserForm) form;
		//首先根据id查找该用户信息
		UserInfo user = getEBookStore().getUserService().findUserById(
				userForm.getId());
		//如果未找到，转向错误页面
		if (user == null) {
			request.setAttribute("message", "未找到该账户");
			return mapping.findForward("failure");
		}
		//如果找到，则设置到表单中
		userForm.setAddress(user.getAddress());
		userForm.setName(user.getName());
		userForm.setUserName(user.getUserName());
		return mapping.findForward("form");
	}
	/**
	 * 保存修改后的密码
	 */
	public ActionForward savePWD(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		UserForm userForm = (UserForm) form;
		//根据该id查找用户
		UserInfo user = getEBookStore().getUserService().findUserById(
				userForm.getId());
		//如果未找到，转向错误页面
		if (user == null) {
			request.setAttribute("message", "未找到该账户");
			return mapping.findForward("failure");
		}
		//如果找到，则设置新密码并保存
		
		user.setPassword(userForm.getPasswrodNew());
		System.out.println("UserAction.savePWD():找到user了，新密码是"+user.getPassword());
		getEBookStore().getUserService().updateUser(user);
		return mapping.findForward("change");
	}
	/**
	 * 转向修改密码页面
	 */
	public ActionForward changePWD(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		return mapping.findForward("change");
	}

}
